﻿Imports System.Data.SqlServerCe

Public Class MetaData
    Public Shared Function GetDBVersion(ByVal source As DataSource) As String
        Return GetValueAsString("VERSION", "0.0", source)
    End Function
    Public Shared Function GetDBVersion(ByVal filePath As String, ByVal password As String) As String
        Return GetValueAsString("VERSION", "0.0", filePath, password)
    End Function
    Friend Shared Sub SetDBVersion(ByVal value As String, ByVal transactionScope As TransactionScope)
        SetValue("VERSION", value, transactionScope)
    End Sub

    Public Shared Function GetValueAsString(ByVal key As String, ByVal [default] As String) As String
        Return GetValueAsString(key, [default], DataSource.Local)
    End Function
    Public Shared Function GetValueAsInteger(ByVal key As String, ByVal [default] As Integer) As Integer
        Dim sVal As String = GetValueAsString(key, [default].ToString, DataSource.Local)
        Return Integer.Parse(sVal)
    End Function
    Public Shared Function GetValueAsBoolean(ByVal key As String, ByVal [default] As Boolean) As Boolean
        Dim sVal As String = GetValueAsString(key, CStr(IIf([default], "1", "0")), DataSource.Local)
        Return sVal = "1"
    End Function

    Private Shared Function GetValueAsString(ByVal key As String, ByVal [default] As String, ByVal source As DataSource) As String
        Dim strSql As String = "SELECT [Value] FROM MetaData WHERE [Key]=@Key"
        Dim sqlCmd As SqlCeCommand = Environment.NewCommand(strSql, source)
        sqlCmd.Parameters.Add("@Key", key)
        sqlCmd.Connection.Open()
        Try
            Using dr As SqlCeDataReader = sqlCmd.ExecuteReader()
                If Not dr.Read Then
                    Return [default]
                Else
                    Return dr.GetString(0)
                End If
            End Using
        Finally
            sqlCmd.Connection.Close()
        End Try
    End Function

    Private Shared Function GetValueAsString(ByVal key As String, ByVal [default] As String, ByVal filePath As String, ByVal password As String) As String
        Dim strSql As String = "SELECT [Value] FROM MetaData WHERE [Key]=@Key"
        Dim sqlCmd As SqlCeCommand = Environment.NewCommand(strSql, filePath, password)
        sqlCmd.Parameters.Add("@Key", key)
        sqlCmd.Connection.Open()
        Try
            Using dr As SqlCeDataReader = sqlCmd.ExecuteReader()
                If Not dr.Read Then
                    Return [default]
                Else
                    Return dr.GetString(0)
                End If
            End Using
        Finally
            sqlCmd.Connection.Close()
        End Try
    End Function

    Public Shared Sub SetValue(ByVal key As String, ByVal value As String)
        Using t As New TransactionScope
            SetValue(key, value, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub SetValue(ByVal key As String, ByVal value As Integer)
        SetValue(key, value.ToString)
    End Sub
    Public Shared Sub SetValue(ByVal key As String, ByVal value As Boolean)
        SetValue(key, CStr(IIf(value, "1", "0")))
    End Sub

    Friend Shared Sub SetValue(ByVal key As String, ByVal value As String, ByVal transactionScope As TransactionScope)
        Dim strSql As String = "UPDATE MetaData SET [Value]=@Value WHERE [Key]=@Key"
        Dim sqlCmd As SqlCeCommand = Environment.NewCommand(strSql, transactionScope)
        sqlCmd.Parameters.Add("@Key", key)
        sqlCmd.Parameters.Add("@Value", value)
        Dim iAffectedRows As Integer = sqlCmd.ExecuteNonQuery()
        If iAffectedRows = 0 Then
            sqlCmd.CommandText = "INSERT INTO MetaData ([Key],[Value]) VALUES (@Key,@Value)"
            iAffectedRows = sqlCmd.ExecuteNonQuery
        End If
    End Sub

End Class
